package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.toolbox.helper.PageHelper;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.saas.domain.dao.SignInDao;
import com.xbongbong.saas.domain.entity.SignInEntity;
import com.xbongbong.saas.model.SignInModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * @author haibin.zhang
 * @version v1.0
 * @date 2019/4/1 13:50
 * @since v1.0
 */
@Service("SignInModel")
public class SignInModelImpl implements SignInModel {

    @Resource
    private SignInDao signInDao;

    @Override
    public Integer save(SignInEntity signInEntity) {
        if (Objects.isNull(signInEntity.getId()) || Objects.equals(signInEntity.getId(), 0L)) {
            return signInDao.insert(signInEntity);
        } else {
            return signInDao.update(signInEntity);
        }
    }

    @Override
    public Integer delete(Long key, String corpid){
        return signInDao.deleteByKey(key, corpid);
    }

    @Override
    public List<SignInEntity> findEntities(Map<String, Object> map) {
        return signInDao.findEntitys(map);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> map) {
        return signInDao.getEntitysCount(map);
    }

    @Override
    public List<Map<String, Object>> getStatistic(Map<String, Object> map, PageHelper pageHelper) {
        //获取签到人数
        Integer rowsCount = signInDao.getUserCount(map);
        pageHelper.setRowsCount(rowsCount);

        Integer pageNum = pageHelper.getPageSize();
        Integer start = (pageHelper.getCurrentPageNum() - 1) * pageNum;

        map.put("orderByStr", "signInCount desc, user_id asc");
        map.put("pageNum", pageNum);
        map.put("start", start);
        List<Map<String, Object>> statisticsMapList = signInDao.getStatistic(map);
        if (statisticsMapList != null && statisticsMapList.size() > 0) {
            Set<String> userIdSet = new HashSet<>();
            userIdSet.add("-1");
            for (Map<String, Object> userMap : statisticsMapList) {
                String userId = (String) userMap.get("userId");
                userIdSet.add(userId);
            }
            map.put("userIdIn", userIdSet);
            map.remove("orderByStr");
            map.remove("start");
            map.remove("pageNum");
            List<Map<String, Object>> customerMapList = signInDao.getStatisticCustomer(map);
            //userId--签到客户数
            Map<String, Integer> tempMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            if (customerMapList != null && customerMapList.size() > 0) {
                for (Map<String, Object> customerMap : customerMapList) {
                    String userId = (String) customerMap.get("userId");
                    Integer customerCount = Integer.valueOf(customerMap.get("customerCount").toString());
                    tempMap.put(userId, customerCount);
                }
            }
            List<Map<String, Object>> data = new ArrayList<>();
            for(Map<String, Object> statisticsMap : statisticsMapList){
                String userId = (String) statisticsMap.get("userId");
                Integer customerCount = tempMap.getOrDefault(userId, 0);
                statisticsMap.put("customerCount", customerCount);
                data.add(statisticsMap);
            }
            return data;
        }
        return new ArrayList<>();
    }

    @Override
    public Integer getUserCount(Map<String, Object> param){
        return signInDao.getUserCount(param);
    }

    /**
     * 通过id获取实体
     *
     * @param id     主键
     * @param corpid 公司id
     * @return SignInEntity
     * @author GuoJun.HU
     * @date 2019/4/19 11:06
     * @since V1.0
     */
    @Override
    public SignInEntity getByKey(Long id, String corpid) {
        return signInDao.getByKey(id, corpid);
    }

    @Override
    public void dataConsistencyUpdateCustomer(String corpid, Long customerId, String customerName) {
        signInDao.dataConsistencyUpdate(corpid, customerId, customerName);
    }

    @Override
    public Integer dataConsistencyUpdateUser(String corpId, String userId, String userName) {
        return signInDao.dataConsistencyUpdateUser(corpId, userId, userName);
    }

    @Override
    public void updateBatch(List<SignInEntity> list, String corpid) {
        if (StringUtil.isEmpty(corpid)) {
            signInDao.updateBatchNoCorpid(list);
        } else {
            signInDao.updateBatch(list, corpid);
        }

    }
}
